<template>
  <div class="index">
    <div class="title">
      <van-search
        class="navbar"
        placeholder="请输入搜索关键词"
        background="#3194ff"
        shape="round"
        @click="$router.push('/search')"
      >
        <template #left>
          <van-icon class="left" name="wap-nav" @click="showPopup" />
        </template>
      </van-search>
    </div>
    <van-tabs
      color="#3296fa"
      title-active-color="#3296fa"
      class="tabs"
      v-model="curIndex"
      sticky
    >
      <van-tab
        :title="channel.name"
        class="tab"
        v-for="channel in channels"
        :key="channel.id"
      >
        <article-list :channel="channel"></article-list>
      </van-tab>
    </van-tabs>
    <!-- 左侧频道管理 -->
    <van-action-sheet v-model="show">
      <!-- 父传子，把当前订阅频道传给频道编辑组件 -->
      <!-- 父传子：自定义属性+props -->
      <!-- 当前频道传递给编辑频道页面 -->
      <channel
        :curIndex="curIndex"
        :channels="channels"
        @fix-curIndex-index="hFixActiveIndex"
        @change-channel="hChangeChannel"
      ></channel>
    </van-action-sheet>
  </div>
</template>

<script>
// 导入API
import { getChannels } from '../../../api/articles'
// 导入子组件
import ArticleList from './articleList.vue' // 文章列表
import channel from './channel.vue' // 频道列表
export default {
  name: 'index',
  components: {
    // 注册组件
    ArticleList,
    channel
  },
  data () {
    return {
      // 控制频道编辑显示与隐藏
      show: false,
      // tabs选中项下标
      curIndex: 0,
      // 上边频道列表
      channels: [],
      // 是否显示频道编辑弹出框
      isMoreActionShow: false
    }
  },
  methods: {
    // 频道编辑显示
    showPopup () {
      this.show = true
    },
    hFixActiveIndex () {
      this.curIndex--
    },
    // 子组件中频道被点击之后切换频道
    hChangeChannel (index) {
      console.log('被点击的频道Id：', index)
      // 1切换频道
      this.curIndex = index
      // 2关闭弹层
      this.show = false
    }
  },
  async created () {
    // 页面加载时渲染上方列表频道
    const res = await getChannels()
    console.log(res)
    this.channels = res.data.data.channels
  }
}
</script>

<style lang="less" scoped>
.index {
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #3194ff;
    .navbar {
      flex: 1;
      .left {
        color: #fff;
        margin-right: 15px;
        font-size: 20px;
      }
    }
    .search {
      padding: 0 10px 0 5px;
      .van-icon,
      .van-icon:before {
        display: inline-block;
        font-size: 20px;
        color: #fff;
        line-height: 33.6px;
      }
    }
  }
  .tabs {
    /deep/.van-tab__text {
      padding: 0 5px;
      font-size: 14px;
    }
  }
  .popup {
    padding: 15px 10px;
    .myicon {
      margin: 15px 0;
      color: #2c3e50;
    }
    .mychannel {
      .tit {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 16px;
        color: #333333;
        .btn {
          font-size: 12px;
          padding: 4px 8px;
          border: 1px solid #f85f5d;
          border-radius: 25px;
          color: #f85f5d;
        }
      }
      .items {
        display: flex;
        flex-wrap: wrap;
        font-size: 14px;
        margin-top: 30px;
        .item {
          padding: 9px;
          background: #f4f5f6;
          color: #222222;
          border-radius: 6px;
          margin-right: 12px;
          margin-bottom: 10px;
        }
      }
    }
    .recommend {
      margin-top: 50px;
      p {
        font-size: 16px;
        color: #333333;
        margin-bottom: 20px;
      }
      .items {
        display: flex;
        flex-wrap: wrap;
        font-size: 14px;
        .item {
          display: flex;
          align-items: center;
          padding: 9px;
          background: #f4f5f6;
          color: #222222;
          border-radius: 6px;
          margin-right: 12px;
          margin-bottom: 10px;
          span {
            padding-left: 5px;
          }
        }
      }
    }
  }
}
</style>
